package cn.xiaolang.function.network.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author Dean
 * @date 2022-01-15
 */
public class SocketClient {

    public static void main(String[] args) {
        try (Socket client = new Socket("127.0.0.1", 10010)) {
            client.setSendBufferSize(20);
            client.setTcpNoDelay(true);
            //创建一个字符输入流缓冲区对象，用转换流把键盘接收到的字节流转换为字符流
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            //获取客户端的字节输出流对象，用来向服务端传输数据
            OutputStream out = client.getOutputStream();
            while (true) {
                String line = br.readLine();
                if (line != null) {
                    byte[] bytes = line.getBytes();
                    for (byte b : bytes) {
                        out.write(b);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
